<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/24
 * Time: 14:50
 */
namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\logic\CateLogic;
use app\admin\logic\GoodLogic;
use app\admin\model\Category;
use app\admin\model\Goods;
use think\facade\Request;
use think\facade\View;

class Good  extends BaseController
{
    public function goodList()
    {
        //获取用户搜索值
        $where=GoodLogic::getWhere();
        $keywords=Request::post('keywords','');
        //分类查询
        $cate_list=Category::getCategory();
        $cate_list=Category::getNextCate($cate_list,0);
        View::assign('cate_list',$cate_list);
        //商品展示
        $list=Goods::getGoodList($where,$keywords);
        View::assign('list',$list);
        return  view();
    }
    //商品添加
    public function goodAdd()
    {
        if (Request::isPost()){
            //添加数据验证和头像上传
            list($msg,$path,$post)=GoodLogic::getGoodAdd();
            if (isset($msg) && $msg != '')    $this->error($msg);
            if ($path !=''){
                $post['photo_x']=$path;
            }
            //添加时间
            $post['addtime']=time();
            //多图数组变为字符串存到数据库
            foreach ($post['photo_string'] as $photo_string){
                $paths[]=str_replace('\\','/',$photo_string);
                $post['photo_string']=implode(',',$paths);
            }
            //商品
            $add=Goods::create($post);
            if ($add){
                $this->success('添加成功',url('Good/goodList'));
            }else{
                $this->error('添加失败');
            }
        }else{
            $cate_list=Category::getCategory();
            $cate_list=Category::getNextCate($cate_list,0);
            View::assign('cate_list',$cate_list);
            return  view();
        }
    }
    //商品修改
    public function goodEdit()
    {
        $id=Request::param('id');
        if (Request::isPost()){
            //商品修改验证器验证和商品头像上传
            list($msg,$path,$post)=GoodLogic::getGoodAdd();
            if (isset($msg)&&$msg != '')    $this->error($msg);
            //当无图时不修改
            if ($path != '')                $post['photo_x']=$path;
            //添加商品修改时间
            $post['updatetime']=time();
            //将上传的多图路径数组变为字符串
            foreach ($post['photo_string'] as $photo_string){
                $paths[]=str_replace('\\','/',$photo_string);
                $post['photo_string']=implode(',',$paths);
            }
            //主键id
            $post['id']=$id;
            //商品数据库修改
            $update=Goods::update($post);
            if ($update){
                $this->success('修改成功',url('Good/goodList'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //当接到主键id时查询
            if ($id){
                $list=Goods::field(true)->find($id)->toArray();
                //将数据库中的商品轮播图变成数组循环展示到前台页面
                $photos=explode(',',$list['photo_string']);
                View::assign('photos',$photos);
                View::assign($list);
            }
            //商品二级分类
            $cate_list=Category::getCategory();
            $cate_list=Category::getNextCate($cate_list,0);
            View::assign('cate_list',$cate_list);
            return  view();
        }
    }
    //商品删除
    public function goodSoftDelete()
    {
        //获取商品id
        $id=Request::param('id');
        if ($id){
            //商品软删
            $delete=Goods::destroy($id);
            if ($delete){
                $this->success('删除成功',url('Good/goodList'));
            }else{
                $this->error('删除失败',url('Good/gooList'));
            }
        }
    }
    //商品回收
    public function goodRecover()
    {
        //处理用户搜索
        $where=GoodLogic::getWhere();
        //用户搜索关键字
        $keywords=Request::param('keywords','');
        //商品二级分类
        $cate_list=Category::getCategory();
        $cate_list=Category::getNextCate($cate_list,0);
        View::assign('cate_list',$cate_list);
        //商品展示
        $list=Goods::getSoftDeleteGoodList($where,$keywords);
        View::assign('list',$list);
        return  view();
    }
    //商品恢复
    public function goodRestore()
    {
        $id=Request::param('id');
        if ($id){
            //恢复被软删除的商品
            $good=Goods::onlyTrashed()->find($id);
            $good->restore();
            if ($good){
                $this->success('恢复成功',url('Good/goodRecover'));
            }else{
                $this->error('恢复失败');
            }
        }
    }
    //商品彻底删除
    public function goodDelete()
    {
        $id=Request::param('id');
        if ($id){
            //商品删除
            $good=Goods::destroy($id,true);
            if ($good){
                $this->success('删除成功',url('Good/goodRecover'));
            }else{
                $this->error('删除失败',url('Good/goodRecover'));
            }
        }
    }
    //分类管理
    public function cate()
    {
        $id=Request::param('id',0);
        if (Request::isPost()){
            //验证用户表单上传数据
            list($msg,$post)=CateLogic::cateData('add');
            if (isset($msg) && $msg != '')      $this->error($msg);
            //验证图片上传
            list($msg,$path)=GoodLogic::getImg('logo','imgs');
            if (isset($msg) && $msg != '')      $this->error($msg);
            if (isset($path) && $path != '')    $post['pic']=$path;
            if (!isset($post['parentid']) || $post['parentid']=='')     $post['parentid']=$id;
            //分类添加时间
            $post['addtime']=time();
            $cate=Category::create($post);
            if ($cate){
                $this->success('添加成功',url('Good/cate'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //二级分类
            $cate_list=Category::getCategory();
            $cate_list=Category::getNextCate($cate_list,$id);
            View::assign('cate_list',$cate_list);
            return  view();
        }
    }
    //分类修改
    public function cateedit()
    {
        $id=Request::param('id');
        if (Request::isPost()){
            $post=Request::post();
            //验证提交数据
            list($msg,$post)=CateLogic::cateData('edit');
            if (isset($msg) && $msg != '')      $this->error($msg);
            //验证上传图片
            list($msg,$path)=GoodLogic::getImg('logo','imgs');
            if (isset($msg) && $msg != '')      $this->error($msg);
            if (isset($path) && $path != '')    $post['pic']=$path;
            //添加id
            $post['id']=$id;
            //用户不传parentid默认为一级分类
            $post['parentid']=$post['parentid']?$post['parentid']:0;
            $res=Category::update($post);
            if ($res){
                $this->success('修改成功',url('Good/cate'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //通过id查找数据
            if ($id){
                $cate=Category::field(true)->find($id)->toArray();
                View::assign($cate);
            }
            //二级分类
            $cate_list=Category::getCategory();
            $cate_list=Category::getNextCate($cate_list,0);
            View::assign('cate_list',$cate_list);
            return  view();
        }

    }
    //分类修改
    public function cateSoftDelete()
    {
        $id=Request::param('id');
        //分类软删除
        if ($id){
            $res=Category::destroy($id);
            if ($res){
                $this->success('删除成功',url('Good/cate'));
            }else{
                $this->error('删除失败',url('Good/cate'));
            }
        }
    }
    //商品多图ajax添加
    public function ajaxImg()
    {
        list($msg,$path)=GoodLogic::getImg('img','UploadFiles/product');
        if (isset($msg)&&$msg != ''){
            return  json(['msg'=>$msg]);
        }else{
            return  json(['msg'=>'成功','path'=>$path]);
        }

    }
}